/* ------------------------------------
Noriduino Reef Lighting Controller

Acknowledgements:- Big thanks to- Dave Rosser, Hugh Dangerfield, Christian, cptbjorn@gmail.com

Purpose:      - to improve upon the work of the Krusduino project
Written for:  - Duemillanove based Krusduino (Temp probe removoed) with Chester's Garage LED Shield v2

Aims:         - to provide smoother ramp up and ramp down functions
              - to allow setting of photoperiod via LCD (removes the need to load from eeprom for cleaner codebase)
              - to allow setting of ramp times (removes the need to load from eeprom for cleaner codebase)
              - to provide 4 channels with graphical representation
              
Changelog:    beta 0.01
              * initial code
              
-------------------------------------*/

// Libraries

#include <Wire.h>         // specify use of Wire.h library.
// #include <Messenger.h>    // This example executes serial commands through a callback function
#include "nokia_3310_lcd.h"

// Definitions ------------------------------------------------------------------------------------

// *************************** User configurable settings start here ******************************

// LED Dimming channels

int WLed1Pin = 6;  //White channel (consider moving due to duty cycle bug on ch 6 & 5)
int Bled1Pin = 5;  //Blue channel
// int Chan3Pin =  //For adding 3rd PWM channel
// int Chan4Pin =  //For adding 4th PWM channel


// Photoperiod and ramp settings

int blueramptime = 60 ;    // minutes after start time to hit bluemax
int whiteramptime = 180 ;  // minutes after start time to hit whitemax
int bluemin = 0 ;          // minimmum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ;        // maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ;         // minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ;       // maximum dimming value of white LEDs, range of 0-255
int photoperiod = 240 ;    // minutes after white hits max to begin fade out
int ontime = 10 ;          // time of day (hour, 24h clock) to begin photoperiod fade in

int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };    // these are the values in 10% increments
int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // these are the values in 10% increments

//****** User config ends here - don't touch anything else unless you know what you are doing ********


//Real Time Clock variables

int secs, mins, hrs, day, date, month, year;

int set_secs = (0); //seconds;
int set_mins = (0) ; //minutes
int set_hrs = (10); //hours (24hr time)
int set_day = (6); // Day 01-07
int set_date = (1); // Date 0-31
int set_month = (10); // month 0-12
int set_year = (9); // Year 00-99

byte CST;


//Nokia 3310

#define NUM_MENU_ITEM	6  // Number of scrollable menu items

// Menu definitions
char menu_items[NUM_MENU_ITEM][12]={
	"NORIDU  ",
        "SET TIME",
        "CYCLE   ",
        "TEST LED",
        "TEST RUN",
        "ABOUT   "
};

void (*menu_funcs[NUM_MENU_ITEM])(void) = {
	menu_noridu,
        menu_set_time,
        menu_cycle,
        menu_test_led,
        menu_test_run,
        menu_about
};

char current_menu_item;

const char RTC_MENU_ITEMS = 9;    // Set time options - Hrs, Mins, Secs, Day, D, M, Y, Ok, Cancel
const char CYCLE_MENU_ITEMS =10;    // Set cycle options - Bmin, Bmax, Wmin, Wmax, Bfade, Wfade, Duration, On time, Ok, Cancel
const char LED_TEST_ITEMS = 7;    // Led test options - (White) ^^^, (Blue) ^^^, Back
const char CYCLE_TEST_ITEMS = 2;  // Cycle test options - Run, Back

// menu starting points
#define MENU_X	10		// 0-83
#define MENU_Y	1		// 0-5


//keypad debounce parameter
#define DEBOUNCE_MAX 15
#define DEBOUNCE_ON  10
#define DEBOUNCE_OFF 3 

#define NUM_KEYS 5

// joystick number
#define UP_KEY 3
#define LEFT_KEY 0
#define CENTER_KEY 1
#define DOWN_KEY 2
#define RIGHT_KEY 4

// adc preset value, represent top value,incl. noise & margin,that the adc reads, when a key is pressed
// set noise & margin = 30 (0.15V@5V)
int  adc_key_val[5] ={30, 150, 360, 535, 760 };

// debounce counters
byte button_count[NUM_KEYS];
// button status - pressed/released
byte button_status[NUM_KEYS];
// button on flags for user program 
byte button_flag[NUM_KEYS];

byte LCD_backlight_pinState = 1;
char tstr[5];  // temporary store for manipulating strings for LCD output

Nokia_3310_lcd lcd=Nokia_3310_lcd();



// Setup --------------------------------------------------------------------------------

void setup() {
  
//pinmode (WLed1Pin, OUTPUT);  //sets up pin as output
//pinmode (BLedPin, OUTPUT);   //sets up pin as output

//RTC setup

//Nokia 3310 setup

   // setup interrupt-driven keypad arrays  
   // reset button arrays
   for(byte i=0; i<NUM_KEYS; i++){
     button_count[i]=0;
     button_status[i]=0;
     button_flag[i]=0;
   }
  
  // Setup timer2 -- Prescaler/256
  TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
  TCCR2B &= ~(1<<WGM22);
  TCCR2B = (1<<CS22)|(1<<CS21);      
  
  ASSR |=(0<<AS2);

   // Use normal mode  
   TCCR2A =0;    
     //Timer2 Overflow Interrupt Enable  
     TIMSK2 |= (0<<OCIE2A);
     TCNT2=0x6;  // counting starts from 6;  
   TIMSK2 = (1<<TOIE2);    
   
                  
 
  SREG|=1<<SREG_I;
  
  lcd.LCD_3310_init();
  lcd.LCD_3310_clear();
  
   //menu initialization
   init_MENU();
   current_menu_item = 0;	



}


// Loop

void loop()  {
  

  
}

// Functions

// Nokia menu stuff

void init_MENU(void){

  byte i;
  
  lcd.LCD_3310_clear();

    lcd.LCD_3310_write_string(MENU_X, MENU_Y, menu_items[0], MENU_HIGHLIGHT );
    
}

// waiting for center key press
void waitfor_OKkey(){
  byte i;
  byte key = 0xFF;
	while (key!= CENTER_KEY){
    for(i=0; i<NUM_KEYS; i++){
       if(button_flag[i] !=0){
           button_flag[i]=0;  // reset button flag
           if(i== CENTER_KEY) key=CENTER_KEY;
        }
     }
   }
		
}

void menu_noridu(){
 
}

void menu_set_time(){
 LCDSetupRTClock();
}

void menu_cycle(){
 LCDSetupCycle();
}

void menu_test_led(){
  LCDSetupLEDTest();       
}

void menu_test_run()
{
  LCDSetupRunTest();       
}


void menu_about(){
  
  lcd.LCD_3310_write_string(0, 0, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 1, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 2, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 3, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 4, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 5, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(54, 5, "  ", MENU_NORMAL);
  
  lcd.LCD_3310_write_string(30, 5, "OK", MENU_HIGHLIGHT );
	waitfor_OKkey();   
}

